package com.starnet.studentmanagement.controller;


import com.starnet.studentmanagement.entity.Students;
import com.starnet.studentmanagement.service.StudentsService;
import com.starnet.studentmanagement.vo.PageResult;
import com.starnet.studentmanagement.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sangeng
 * @since 2022-03-26
 */
@Api(value = "学生管理")
@RestController
@RequestMapping("/students")
public class StudentsController {
    @Autowired
    StudentsService studentsService;

    @ApiOperation(value = "根据姓名分页查询学生")
    @GetMapping()
    public PageResult searchByName(@RequestParam(value = "sname",required = false)String sname,@RequestParam("page")Integer page,@RequestParam(value = "pageSize",required = false)Integer pageSize) {
        return studentsService.searchByName(sname, page,pageSize);
    }

    @ApiOperation(value = "新增学生")
    @PostMapping
    public Result addNewStudent(@RequestBody @Valid Students student) {
        return studentsService.addNewStudent(student);
    }

    @ApiOperation(value = "更新学生")
    @PutMapping
    public Result updateStudent(@RequestBody Students student) {
        return studentsService.updateStudent(student);
    }

    @ApiOperation(value = "删除学生")
    @DeleteMapping("/{id}")
    public Result deleteBySid(@PathVariable("id") int id) {
        return studentsService.deleteById(id);
    }

}
